<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="exchangeFormRules" :model="exchangeForm" label-position="left" label-width="140px">
      <el-form-item label="编码" prop="code">
        <el-input v-model="exchangeForm.code" />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="exchangeForm.title" />
      </el-form-item>
      <el-form-item label="描述" prop="remark">
        <el-input v-model="exchangeForm.remark" :autosize="{ minRows: 2, maxRows: 4}" type="textarea" placeholder="备注" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCannel()">
        取消
      </el-button>
      <el-button type="primary" @click="createData()">
        确认
      </el-button>
      <el-button v-if="exchangeForm.status.value === 5" type="danger" @click="handleDelete(exchangeForm)">
        删除
      </el-button>
    </div>
  </div>
</template>

<script>
import { saveExchange, removeExchangeById, getExchange } from '@/api/knowledge/exchange'
import waves from '@/directive/waves' // waves directive
import { } from '@/utils'

export default {
  name: 'ComplexTable',
  components: { },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      tableKey: 0,
      exchangeForm: {
        code: '',
        title: '',
        remark: '',
        status: {
          label: '',
          value: 1
        }
      },
      exchangeFormRules: []
    }
  },
  created() {
    let exchangeID = this.$route.query && this.$route.query.exchangeID
    if (exchangeID !== undefined) {
      exchangeID = parseInt(exchangeID)
      if (!isNaN(exchangeID)) {
        this.listLoading = true
        const formData = {
          'exchangeID': exchangeID
        }
        getExchange(formData).then(response => {
          if (response.status === 200) {
            this.exchangeForm = response.data
          } else {
            this.$message({
              message: response.msg,
              type: 'error'
            })
          }
        })
      }
    }
    console.log(exchangeID)
  },
  methods: {
    handleCannel() {
      this.$router.push({ path: 'exchange' })
    },
    createData() {
      this.listLoading = true
      const exchangeForm = this.exchangeForm
      exchangeForm.status = exchangeForm.status.value
      console.log(exchangeForm)
      if (exchangeForm.ID > 0) {
        exchangeForm.exchangeID = exchangeForm.ID
      }
      saveExchange(exchangeForm).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'exchange' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    },
    handleDelete(row, index) {
      const fromdata = {
        'exchangeID': row.ID
      }
      removeExchangeById(fromdata).then(response => {
        if (response.status === 200) {
          this.$message({
            message: '操作Success',
            type: 'success'
          })
          this.$router.push({ path: 'exchange' })
        } else {
          this.$message({
            message: response.msg,
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
